﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Data;
using Ultima.Xna.Extensions;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI
{
    public class GumpTextEntry : GumpLabel
    {
        private bool _masked = false;
        private string _text = string.Empty;
        private int _maxLength = 255;

        public int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        public bool Masked
        {
            get { return _masked; }
            set { _masked = value; }
        }

        public override Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public override string Text
        {
            get { return Utility.WrapASCIIText(FontId, _text, _size.X); }
            set { _text = value; }
        }

        public override bool Moveable
        {
            get { return false; }
        }

        public GumpTextEntry(Game game, IUIContainerNode parent)
            : base(game, parent)
        {

        }

        public override void HandleKeyDown(KeyboardEventArgs e)
        {
            if (_defaultBehaviour)
            {
                _manager.Input.HandleKeyboardInput(ref _text);

                if (_text.Length > _maxLength)
                {
                    _text = _text.Substring(0, _maxLength);
                }
            }
            else
            {
                base.HandleKeyDown(e);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            PresentationParameters pp = _graphicsDevice.PresentationParameters;

            _spriteBatch.End();

            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["HueTexture"].SetValue(Hues.Texture);
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(Hue - 1, 0));
            }

            _spriteBatch.BeginLast();

            RectangleF renderBounds;
            CalculateRenderBounds(out renderBounds);

            _graphicsDevice.ScissorRectangle = renderBounds;
            _graphicsDevice.RenderState.ScissorTestEnable = true;

            ASCIIFont font = Font;
            string text = Text;
            Vector2 position = new Vector2(renderBounds.X, renderBounds.Y);
            Vector2 fontSize = new Vector2(font.GetWidth(text), (font.Height * (text.CountChar('\n') + 1)));

            float dx = 0;
            float height = font.Height;

            string[] split = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < split.Length; i++)
            {
                dx = 0;

                for (int x = 0; x < split[i].Length; x++)
                {
                    if (_masked)
                    {
                        int count = split[i].Length;
                        split[i] = string.Empty;

                        for (int a = 0; a < split[i].Length; a++)
                            string.Concat(split[i], "*");
                    }

                    _spriteBatch.Draw(font.GetTexture(split[i][x]), position + new Vector2(dx, i * font.Height), Color.White);
                    dx += font.GetWidth(split[i][x].ToString());
                }
            }

            if (_spriteBatch.Effect != null)
            {
                _spriteBatch.Effect.Parameters["Hue"].SetValue(new Vector2(-1, 0));
            }

            _spriteBatch.End();
            _spriteBatch.BeginLast();
            
            _graphicsDevice.ScissorRectangle = new Rectangle(0, 0, pp.BackBufferWidth, pp.BackBufferHeight);
            _graphicsDevice.RenderState.ScissorTestEnable = false;

            base.Draw(gameTime);
        }
    }
}
